package net.kleinhenz.norabase.client;

import net.kleinhenz.norabase.shared.model.Champion;
import net.kleinhenz.norabase.shared.model.Equipment;
import net.kleinhenz.norabase.shared.model.Relic;
import net.kleinhenz.norabase.shared.model.Spell;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.util.SC;

public class RuneLoader {

	public static void loadChampion(final Integer id, final FrontCard frontCard, final BackCard backCard, final RatingCanvas cardCommentsTab) {
		RuneServiceAsync service = GWT.create(RuneService.class);
		service.fetchChampion(id, new AsyncCallback<Champion>() {
			public void onFailure(Throwable caught) {
				SC.say("An error occured while retrieving rune info from the server. Please try again.");
			}
			public void onSuccess(Champion result) {
				frontCard.loadRune(result);
				backCard.loadRune(result);
				if (cardCommentsTab!=null)
					cardCommentsTab.loadRune(result);
			}
		});
	}

	public static void loadSpell(final Integer id, final FrontCard frontCard, final BackCard backCard, final RatingCanvas cardCommentsTab) {
		RuneServiceAsync service = GWT.create(RuneService.class);
		service.fetchSpell(id, new AsyncCallback<Spell>() {
			public void onFailure(Throwable caught) {
				SC.say("An error occured while retrieving rune info from the server. Please try again.");
			}
			public void onSuccess(Spell result) {
				frontCard.loadRune(result);
				backCard.loadRune(result);
				if (cardCommentsTab!=null)
					cardCommentsTab.loadRune(result);
			}
		});
	}

	public static void loadEquipment(final Integer id, final FrontCard frontCard, final BackCard backCard, final RatingCanvas cardCommentsTab) {
		RuneServiceAsync service = GWT.create(RuneService.class);
		service.fetchEquipment(id, new AsyncCallback<Equipment>() {
			public void onFailure(Throwable caught) {
				SC.say("An error occured while retrieving rune info from the server. Please try again.");
			}
			public void onSuccess(Equipment result) {
				frontCard.loadRune(result);
				backCard.loadRune(result);
				if (cardCommentsTab!=null)
					cardCommentsTab.loadRune(result);
			}
		});
	}
	
	public static void loadRelic(final Integer id, final FrontCard frontCard, final BackCard backCard, final RatingCanvas cardCommentsTab) {
		RuneServiceAsync service = GWT.create(RuneService.class);
		service.fetchRelic(id, new AsyncCallback<Relic>() {
			public void onFailure(Throwable caught) {
				SC.say("An error occured while retrieving rune info from the server. Please try again.");
			}
			public void onSuccess(Relic result) {
				frontCard.loadRune(result);
				backCard.loadRune(result);
				if (cardCommentsTab!=null)
					cardCommentsTab.loadRune(result);
			}
		});
	}
}
